package com.orchid.community.models.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.orchid.community.common.utils.CodeEnum;
import com.orchid.community.common.utils.MsgResponse;
import com.orchid.community.common.utils.ResultUtils;
import com.orchid.community.models.DTO.TagListRequestDTO;
import com.orchid.community.models.DTO.TagRequestDTO;
import com.orchid.community.models.entity.Tags;
import com.orchid.community.models.mapper.TagsMapper;
import com.orchid.community.models.service.TagsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class TagsServiceImpl implements TagsService {

    @Resource
    private TagsMapper tagsMapper;
    @Override
    public MsgResponse createTag(TagRequestDTO request) {
        int count = validateTagExist(request.getName());
        if (count > 0) {
            return ResultUtils.fail(CodeEnum.DATA_ERROR);
        }
        if (request.getName().length() > 10) {
            return ResultUtils.fail("字符串长度最大为" + 10);
        }
        Tags tags = new Tags();
        tags.setName(request.getName());
        tags.setKind(request.getKind());
        tags.setDelFlag(0);
        tags.setCreateDate(new Date());
        tags.setStatus(1);
        tagsMapper.createTag(tags);
        return ResultUtils.success();
    }

    @Override
    public MsgResponse deleteTag(TagRequestDTO request) {
        Tags tag = tagsMapper.getById(request.getId());
        if (tag == null) {
            return ResultUtils.fail("标签不存在");
        }
        tag.setDelFlag(1);
        tagsMapper.updateById(tag);
        return ResultUtils.success();
    }

    @Override
    public MsgResponse updateTag(TagRequestDTO request) {
        Tags tag = tagsMapper.getById(request.getId());
        if (tag == null) {
            return ResultUtils.fail("标签不存在");
        }
        // id不等于当前更新的id
        Tags validateTag = tagsMapper.selectByName(request.getName(), tag.getId());
        // 查询出来重名的标签
        if (validateTag !=null ) {
            return ResultUtils.fail(CodeEnum.DATA_ERROR);
        }
        tag.setName(request.getName());
        tagsMapper.updateById(tag);
        return ResultUtils.success();
    }

    @Override
    public MsgResponse setStatus(TagRequestDTO request) {
        if (request.getStatus() != 1 && request.getStatus() != 0) {
            return ResultUtils.fail("参数不符合规范");
        }
        Tags tag = tagsMapper.getById(request.getId());
        if (tag == null) {
            return ResultUtils.fail("标签不存在");
        }
        tag.setStatus(request.getStatus());
        tagsMapper.updateById(tag);
        return ResultUtils.success();
    }

    @Override
    public MsgResponse getPageList(TagListRequestDTO tagListRequestDTO) {
        PageHelper.startPage(tagListRequestDTO.getPageNum(), tagListRequestDTO.getPageSize());
        List<TagRequestDTO> pageList = tagsMapper.getPageList(tagListRequestDTO.getParam());
//        log.info("查询结果:{}", pageList);
        PageInfo<TagRequestDTO> pageInfo = new PageInfo<>(pageList);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return ResultUtils.success(resultMap);
    }


    public int validateTagExist(String name) {
        if (StrUtil.isBlank(name)) {
            throw new RuntimeException("标签不存在");
        }
        return tagsMapper.countByName(name);
    }
}
